<script type="text/javascript">
    (function () {
        function hasProperty (obj, prop) {
            return Object.prototype.hasOwnProperty.call(obj, prop)
        }
        RED.nodes.registerType('ui-button', {
            category: RED._('@flowfuse/node-red-dashboard/ui-base:ui-base.label.category'),
            color: RED._('@flowfuse/node-red-dashboard/ui-base:ui-base.colors.light'),
            defaults: {
                group: { type: 'ui-group', required: true },
                name: { value: '' },
                label: { value: RED._('@flowfuse/node-red-dashboard/ui-button:ui-button.label.button') },
                order: { value: 0 },
                width: {
                    value: 0,
                    validate: function (v) {
                        const width = v || 0
                        const currentGroup = $('#node-input-group').val() || this.group
                        const groupNode = RED.nodes.node(currentGroup)
                        const valid = !groupNode || +width <= +groupNode.width
                        $('#node-input-size').toggleClass('input-error', !valid)
                        return valid
                    }
                },
                height: { value: 0 },
                emulateClick: { value: false },
                tooltip: { value: '' },
                color: { value: '' },
                bgcolor: { value: '' },
                className: { value: '' },
                icon: { value: '' },
                iconPosition: { value: 'left' },
                payload: { value: '', validate: (hasProperty(RED.validators, 'typedInput') ? RED.validators.typedInput('payloadType') : function (v) { return true }) },
                payloadType: { value: 'str' },
                topic: { value: 'topic', validate: (hasProperty(RED.validators, 'typedInput') ? RED.validators.typedInput('topicType') : function (v) { return true }) },
                topicType: { value: 'msg' }
            },
            inputs: 1,
            outputs: 1,
            outputLabels: function () {
                if (this.payloadType === 'str') {
                    return this.payload
                } else {
                    return this.payloadType
                }
            },
            icon: 'font-awesome/fa-hand-pointer-o',
            paletteLabel: 'button',
            label: function () { return this.name || (~this.label.indexOf('{' + '{') ? null : this.label) || 'button' },
            labelStyle: function () { return this.name ? 'node_label_italic' : '' },
            oneditprepare: function () {
                // if this groups parent is a subflow template, set the node-config-input-width and node-config-input-height up
                // as typedInputs and hide the elementSizer (as it doesn't make sense for subflow templates)
                if (RED.nodes.subflow(this.z)) {
                    // change inputs from hidden to text & display them
                    $('#node-input-width').attr('type', 'text')
                    $('#node-input-height').attr('type', 'text')
                    $('div.form-row.nr-db-ui-element-sizer-row').hide()
                    $('div.form-row.nr-db-ui-manual-size-row').show()
                } else {
                    // not in a subflow, use the elementSizer
                    $('div.form-row.nr-db-ui-element-sizer-row').show()
                    $('div.form-row.nr-db-ui-manual-size-row').hide()
                    $('#node-input-size').elementSizer({
                        width: '#node-input-width',
                        height: '#node-input-height',
                        group: '#node-input-group'
                    })
                }

                $('#node-input-payload').typedInput({
                    default: 'str',
                    typeField: $('#node-input-payloadType'),
                    types: ['str', 'num', 'bool', 'json', 'bin', 'date', 'flow', 'global']
                })

                $('#node-input-topic').typedInput({
                    default: 'str',
                    typeField: $('#node-input-topicType'),
                    types: ['str', 'msg', 'flow', 'global']
                })

                if (!this.iconPosition) {
                    $('#node-input-iconPosition').val('left')
                }

                // use jQuery UI tooltip to convert the plain old title attribute to a nice tooltip
                $('.ui-node-popover-title').tooltip({
                    show: {
                        effect: 'slideDown',
                        delay: 150
                    }
                })
            }
        })
    })()
</script>

<script type="text/html" data-template-name="ui-button">
    <div class="form-row">
        <label for="node-input-group"><i class="fa fa-table"></i> <span data-i18n="ui-button.label.group"></label>
        <input type="text" id="node-input-group">
    </div>
    <div class="form-row nr-db-ui-element-sizer-row">
        <label><i class="fa fa-object-group"></i> <span data-i18n="ui-button.label.size"></label>
        <button class="editor-button" id="node-input-size"></button>
    </div>
    <div class="form-row nr-db-ui-manual-size-row">
        <label><i class="fa fa-arrows-h"></i> <span data-i18n="ui-button.label.width">Width</label>
        <input type="hidden" id="node-input-width">
    </div>
    <div class="form-row nr-db-ui-manual-size-row">
        <label><i class="fa fa-arrows-v"></i> <span data-i18n="ui-button.label.height">Height</label>
        <input type="hidden" id="node-input-height">
    </div>
    <div class="form-row form-row-flex">
        <label for="node-input-icon"><i class="fa fa-picture-o"></i> <span data-i18n="ui-button.label.icon"></label>
        <div style="display: flex; align-items: center; flex-grow: 1; padding-right: calc(30% - 104px); gap: 12px;">
            <input style="flex-grow: 1;" type="text" id="node-input-icon" data-i18n="[placeholder]ui-button.label.optionalIcon">
            <select id="node-input-iconPosition" style="width: 125px;">
                <option value="left" data-i18n="ui-button.label.left"></option>
                <option value="right" data-i18n="ui-button.label.right"></option>
            </select>
        </div>
    </div>
    <div class="form-row">
        <label for="node-input-label"><i class="fa fa-i-cursor"></i> <span data-i18n="ui-button.label.label"></label>
        <input type="text" id="node-input-label" data-i18n="[placeholder]ui-button.label.optionalLabel">
    </div>
    <div class="form-row">
        <label for="node-input-className"><i class="fa fa-code"></i> <span data-i18n="ui-button.label.className"></label>
        <div style="display: inline;">
            <input style="width: 70%;" type="text" id="node-input-className" data-i18n="[placeholder]ui-button.label.classNamePlaceholder" style="flex-grow: 1;">
            <a
                data-html="true"
                title="Dynamic Property: Send msg.class to append new classes to this widget. NOTE: classes set at runtime will be applied in addition to any class(es) set in the nodes class field."
                class="red-ui-button ui-node-popover-title"
                style="margin-left: 4px; cursor: help; font-size: 0.625rem; border-radius: 50%; width: 24px; height: 24px; display: inline-flex; justify-content: center; align-items: center;">
                <i style="font-family: ui-serif;">fx</i>
            </a>
        </div>
    </div>
    <!--<div class="form-row">
        <label for="node-input-tooltip"><i class="fa fa-info-circle"></i> <span data-i18n="ui-button.label.tooltip"></label>
        <input type="text" id="node-input-tooltip" data-i18n="[placeholder]ui-button.label.optionalTooltip">
    </div>
    <div class="form-row">
        <label for="node-input-color"><i class="fa fa-tint"></i> <span data-i18n="ui-button.label.color"></label>
        <input type="text" id="node-input-color" data-i18n="[placeholder]ui-button.label.optionalColor">
    </div>
    <div class="form-row">
        <label for="node-input-bgcolor"><i class="fa fa-tint"></i>  <span data-i18n="ui-button.label.background"></label>
        <input type="text" id="node-input-bgcolor" data-i18n="[placeholder]ui-button.label.optionalBackgroundColor">
    </div>-->
    <div class="form-row">
        <label style="width:auto" for="node-input-payload"><i class="fa fa-envelope-o"></i> <span data-i18n="ui-button.label.whenClicked"></label>
    </div>
    <div class="form-row">
        <label for="node-input-payload" style="padding-left: 25px; margin-right: -25px"><span data-i18n="ui-button.label.payload"></label>
        <input type="text" id="node-input-payload" style="width:70%">
        <input type="hidden" id="node-input-payloadType">
    </div>
    <div class="form-row">
        <label for="node-input-topic" style="padding-left: 25px; margin-right: -25px"><span data-i18n="ui-button.label.topic"></label>
        <input type="text" id="node-input-topic" style="width:70%">
        <input type="hidden" id="node-input-topicType">
    </div>
    <div class="form-row">
        <label style="width:auto" for="node-input-emulateClick"><i class="fa fa-arrow-right"></i> <span data-i18n="ui-button.label.emulateClick"></label>
        <input type="checkbox" id="node-input-emulateClick" style="display:inline-block; width:auto; vertical-align:top;">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
    </div>
</script>